<template>
  <j-modal
    :title="title"
    :width="width"
    :visible="visible"
    :confirmLoading="confirmLoading"
    switchFullscreen
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-row>
          <!--              <a-col :xs="24" :sm="12">-->
          <!--                <a-form-item label="所属单位" :labelCol="labelCol" :wrapperCol="wrapperCol">-->
          <!--                  <j-select-category v-decorator="['assetFixedCategoryId']"></j-select-category>-->
          <!--                </a-form-item>-->
          <!--              </a-col>-->
          <a-col :xs="24" :sm="12">
            <a-form-item label="物资分类" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-search-select-tag    @change="toSet"  type="list" v-decorator="['assetType',validatorRules.assetType]"
                                   :trigger-change="true"  dict="gf_zcgl_asset_type" :disabled="!addFlag"
                                   placeholder="请选择物资分类"/>
            </a-form-item>
          </a-col>
          <a-col :xs="24" :sm="12"   v-if="xtype">
            <a-form-item label="子分类" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-search-select-tag type="list" v-decorator="['bgypType',validatorRules.bgypType]"
                                   :trigger-change="true"  dict="bgyp_type"
                                   :disabled="!addFlag"
                                   placeholder="请选择子分类"/>
            </a-form-item>
          </a-col>
          <a-col :xs="24" :sm="12"   v-if="cwhzxtype">
            <a-form-item label="子分类" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-search-select-tag type="list" v-decorator="['chwzType',validatorRules.bgypType]"
                                   :trigger-change="true"  dict="chwz_type"  v-if="addFlag"
                                   placeholder="请选择子分类"/>
            </a-form-item>
          </a-col>
<!--              <a-col  :xs="24" :sm="12"  v-if="!yhp">-->
<!--                          <a-form-item-->
<!--                            :labelCol="labelCol"-->
<!--                            :wrapperCol="wrapperCol"-->
<!--                            label="物资编码">-->
<!--                            <a-input   disabled="true" placeholder="请输入产品编码" v-decorator="['code', validatorRules.code]" />-->
<!--                          </a-form-item>-->
<!--             </a-col>-->
          <a-col :xs="24" :sm="12">
            <a-form-item
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              label="物资名称">
              <a-input placeholder="请输入物资名称" v-decorator="['name', validatorRules.name]" />
            </a-form-item>
          </a-col>
          <a-col :xs="24" :sm="12">
            <a-form-item
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              label="规格型号">
              <a-input placeholder="请输入规格型号" v-decorator="['specifications',validatorRules.specifications]" />
            </a-form-item>
          </a-col>
          <a-col :xs="24" :sm="12">
            <!--                <a-form-item-->
            <!--                  :labelCol="labelCol"-->
            <!--                  :wrapperCol="wrapperCol"-->
            <!--                  label="单位">-->
            <!--                  <a-input placeholder="请输入单位" v-decorator="['unit', {}]" />-->
            <!--                </a-form-item>-->
            <a-form-item label="单位" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-search-select-tag type="list" v-decorator="['unit',validatorRules.unit]"
                                   :trigger-change="true"  dict="zcgl_dw" :disabled="disableSubmit"
                                   placeholder="请选择物资单位"/>
            </a-form-item>
          </a-col>
<!--          <a-col :xs="24" :sm="12">-->
<!--            <a-form-item-->
<!--              :labelCol="labelCol"-->
<!--              :wrapperCol="wrapperCol"-->
<!--              label="数量">-->
<!--              <a-input-number placeholder="请输入数量" v-decorator="['amount', {}]" style="width: 100%"/>-->
<!--            </a-form-item>-->
<!--          </a-col>-->

          <a-col :xs="24" :sm="12">

            <a-form-item label="安全库存" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input-number placeholder="请输入安全库存" v-decorator="['jiexian', validatorRules.jiexian]" style="width: 100%"/>
            </a-form-item>
          </a-col>


<!--          <a-col :xs="24" :sm="12">-->
<!--            <a-form-item-->
<!--              :labelCol="labelCol"-->
<!--              :wrapperCol="wrapperCol"-->
<!--              label="单价">-->
<!--              <a-input-number v-decorator="[ 'price', {}]" style="width: 100%"/>-->
<!--            </a-form-item>-->
<!--          </a-col>-->
          <!--              <a-col :xs="24" :sm="12">-->
          <!--                <a-form-item-->
          <!--                  :labelCol="labelCol"-->
          <!--                  :wrapperCol="wrapperCol"-->
          <!--                  label="合价">-->
          <!--                  <a-input-number v-decorator="[ 'total', {}]" style="width: 100%"/>-->
          <!--                </a-form-item>-->
          <!--              </a-col>-->
          <!--              <a-col :xs="24" :sm="12">-->
          <!--                <a-form-item-->
          <!--                  :labelCol="labelCol"-->
          <!--                  :wrapperCol="wrapperCol"-->
          <!--                  label="采购部门">-->
          <!--                  <a-input placeholder="请输入采购部门" v-decorator="['purchasingDepartment', {}]" />-->
          <!--                </a-form-item>-->
          <!--              </a-col>-->
          <!--              <a-col :xs="24" :sm="12">-->
          <!--                <a-form-item-->
          <!--                  :labelCol="labelCol"-->
          <!--                  :wrapperCol="wrapperCol"-->
          <!--                  label="采购日期">-->
          <!--                  <a-date-picker showTime format="YYYY-MM-DD HH:mm:ss" v-decorator="[ 'purchaseDate', {}]" />-->
          <!--                </a-form-item>-->
          <!--              </a-col>-->
          <a-col :xs="24" :sm="12">
            <a-form-item
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              label="图片">
              <!--            <a-input placeholder="请输入图片" v-decorator="['image', {}]" />-->
              <!--            <j-image-upload  v-decorator="['image', {}]"></j-image-upload>-->
              <j-image-upload text="上传" v-decorator="['image', {}]" :isMultiple="true" :number="6"></j-image-upload>
            </a-form-item>
          </a-col>
          <!--              <a-col :xs="24" :sm="12">-->
          <!--                <a-form-item-->
          <!--                  :labelCol="labelCol"-->
          <!--                  :wrapperCol="wrapperCol"-->
          <!--                  label="材质说明">-->
          <!--                  <a-input placeholder="请输入材质说明" v-decorator="['description', {}]" />-->
          <!--                </a-form-item>-->
          <!--              </a-col>-->
          <!--              <a-col :xs="24" :sm="12">-->
          <!--                <a-form-item-->
          <!--                  :labelCol="labelCol"-->
          <!--                  :wrapperCol="wrapperCol"-->
          <!--                  label="楼层">-->
          <!--                  <a-input placeholder="请输入楼层" v-decorator="['floor', {}]" />-->
          <!--                </a-form-item>-->
          <!--              </a-col>-->
          <!--              <a-col :xs="24" :sm="12">-->
          <!--                <a-form-item-->
          <!--                  :labelCol="labelCol"-->
          <!--                  :wrapperCol="wrapperCol"-->
          <!--                  label="尺寸">-->
          <!--                  <a-input v-decorator="[ 'assetSize', {}]" />-->
          <!--                </a-form-item>-->
          <!--              </a-col>-->
          <!--              <a-col :xs="24" :sm="12">-->
          <!--                <a-form-item-->
          <!--                  :labelCol="labelCol"-->
          <!--                  :wrapperCol="wrapperCol"-->
          <!--                  label="款式">-->
          <!--                  <a-input placeholder="请输入款式" v-decorator="['assetStyle', {}]" />-->
          <!--                </a-form-item>-->
          <!--              </a-col>-->
          <!--              <a-col :xs="24" :sm="12">-->
          <!--                <a-form-item-->
          <!--                  :labelCol="labelCol"-->
          <!--                  :wrapperCol="wrapperCol"-->
          <!--                  label="技术参数">-->
          <!--                  <a-input placeholder="请输入技术参数" v-decorator="['technicalParameters', {}]" />-->
          <!--                </a-form-item>-->
          <!--              </a-col>-->
          <!--              <a-col :xs="24" :sm="12">-->
          <!--                <a-form-item-->
          <!--                  :labelCol="labelCol"-->
          <!--                  :wrapperCol="wrapperCol"-->
          <!--                  label="品牌">-->
          <!--                  <a-input placeholder="请输入品牌" v-decorator="['brand', {}]" />-->
          <!--                </a-form-item>-->
          <!--              </a-col>-->
          <a-col :span =24>
            <a-form-item
              :labelCol="labelCol2"
              :wrapperCol="wrapperCol2"
              label="备注">
              <a-textarea placeholder="请输入备注" v-decorator="['remark', {}]"  style="height: 80px"/>
            </a-form-item>
          </a-col>
          <!--              <a-divider>折旧信息</a-divider>-->
          <!--              <a-col :xs="24" :sm="12">-->
          <!--                <a-form-item-->
          <!--                  :labelCol="labelCol"-->
          <!--                  :wrapperCol="wrapperCol"-->
          <!--                  label="原值">-->
          <!--                  <a-input-number v-decorator="[ 'originalCost', {}]" style="width: 100%"/>-->
          <!--                </a-form-item>-->
          <!--              </a-col>-->
          <!--              <a-col :xs="24" :sm="12">-->
          <!--                <a-form-item-->
          <!--                  :labelCol="labelCol"-->
          <!--                  :wrapperCol="wrapperCol"-->
          <!--                  label="累计折旧">-->
          <!--                  <a-input-number v-decorator="[ 'accumulativeDiscount', {}]" style="width: 100%"/>-->
          <!--                </a-form-item>-->
          <!--              </a-col>-->
          <!--              <a-col :xs="24" :sm="12">-->
          <!--                <a-form-item-->
          <!--                  :labelCol="labelCol"-->
          <!--                  :wrapperCol="wrapperCol"-->
          <!--                  label="净残值率">-->
          <!--                  <a-input-number v-decorator="[ 'residualValueRate', {}]" style="width: 100%" @blur="calculationResidualValue"/>-->
          <!--                </a-form-item>-->
          <!--              </a-col>-->
          <!--              <a-col :xs="24" :sm="12">-->
          <!--                <a-form-item-->
          <!--                  :labelCol="labelCol"-->
          <!--                  :wrapperCol="wrapperCol"-->
          <!--                  label="净残值">-->
          <!--                  <a-input-number v-decorator="[ 'residualValue', {}]" style="width: 100%"/>-->
          <!--                </a-form-item>-->
          <!--              </a-col>-->
          <!--              <a-col :xs="24" :sm="12">-->
          <!--                <a-form-item-->
          <!--                  :labelCol="labelCol"-->
          <!--                  :wrapperCol="wrapperCol"-->
          <!--                  label="折旧方法">-->
          <!--                  <j-dict-select-tag  v-decorator="['depreciationMethod', {}]" :triggerChange="true" placeholder="请输入用户性别"-->
          <!--                                      dictCode="depreciation_method"/>-->
          <!--                </a-form-item>-->
          <!--              </a-col>-->
          <!--              <a-col :xs="24" :sm="12">-->
          <!--                <a-form-item-->
          <!--                  :labelCol="labelCol"-->
          <!--                  :wrapperCol="wrapperCol"-->
          <!--                  label="月折旧率">-->
          <!--                  <a-input-number v-decorator="[ 'monthDepreciationRate', {}]" style="width: 100%" @blur="calculationMonthDepreciation"/>-->
          <!--                </a-form-item>-->
          <!--              </a-col>-->
          <!--              <a-col :xs="24" :sm="12">-->
          <!--                <a-form-item-->
          <!--                  :labelCol="labelCol"-->
          <!--                  :wrapperCol="wrapperCol"-->
          <!--                  label="月折旧额">-->
          <!--                  <a-input-number v-decorator="[ 'monthDepreciation', {}]" style="width: 100%"/>-->
          <!--                </a-form-item>-->
          <!--              </a-col>-->
          <!--              <a-col :xs="24" :sm="12">-->
          <!--                <a-form-item-->
          <!--                  :labelCol="labelCol"-->
          <!--                  :wrapperCol="wrapperCol"-->
          <!--                  label="预计使用年限">-->
          <!--                  <a-input-number v-decorator="[ 'expectedServiceLife', {}]" style="width: 100%"/>-->
          <!--                </a-form-item>-->
          <!--              </a-col>-->
        </a-row>
      </a-form>
    </a-spin>

    <template slot="footer"  >
      <a-button @click="handleCancel">关闭</a-button>
<!--      v-if="addFlag"-->
      <a-button    @click="handleOk">确定</a-button>
    </template>
  </j-modal>
</template>

<script>

  import { httpAction } from '@/api/manage'
  import pick from 'lodash.pick'
  import { validateDuplicateValue } from '@/utils/util'
  import JSelectCategory from '../../../components/jeecgbiz/JSelectCategory'
  import JImageUpload from '../../../components/jeecg/JImageUpload'
  import JSearchSelectTag from '../../../components/dict/JSearchSelectTag'
  import JSelectCangKu from '../../../components/jeecgbiz/JSelectCangKu'


  export default {
    name: "ZcglSuppliesModal",
    components: { JSelectCategory, JImageUpload,JSearchSelectTag,JSelectCangKu },
    props: {


      addFlag: {
        type: Object,
        default: ()=>{}
      },

    },
    data () {
      return {
        yhp:false,
        xtype:false,
        cwhzxtype:false,
        form: this.$form.createForm(this),
        title:"操作",
        width:800,
        visible: false,
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
          code: {
            rules: [
              { required: true, message: '请输入编码!'},
            ]
          },
          bgypType: {
            rules: [
              { required: true, message: '请选择子分类!'},
            ]
          },
          jiexian: {
            rules: [
              { required: false, message: '请输入安全库存!'},
            ]
          },
          assetType: {
            rules: [
              { required: true, message: '请选择分类!'},
            ]
          },
          name: {
            rules: [
              { required: true, message: '请填写名称!'},
            ]
          },
          specifications: {
            rules: [
              { required: false, message: '请填写规格!'},
            ]
          },
          unit: {
            rules: [
              { required: true, message: '请选择单位!'},
            ]
          },
        },
        url: {
          add: "/zcgl/assetFixed/add",
          edit: "/zcgl/assetFixed/edit",
        }
      }
    },
    created () {
    },
    methods: {
      toSet(a){
        if(a.indexOf('BGYP')!=-1){
          this.xtype = true

        }else{
          this.xtype = false

        }
        if(a.indexOf('YHP')!=-1){
          this.yhp = true

        }else{
          this.yhp = false

        }
        if(a.indexOf('CHWZ')!=-1){
          this.cwhzxtype = true

        }else{
          this.cwhzxtype = false

        }




      },
      add () {
        this.edit({});
      },
      edit (record) {
        if(record.assetType!=null){


          if(record.assetType.indexOf('BGYP')!=-1){
            this.xtype = true

          }else{
            this.xtype = false

          }
          if(record.assetType.indexOf('CHWZ')!=-1){
            this.cwhzxtype = true

          }else{
            this.cwhzxtype = false

          }

        }




        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,'code','image','name','assetType','specifications','unit','jiexian','price','bgypType','chwzType'))
        })
      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
      handleOk () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            let formData = Object.assign(this.model, values);
            console.log("表单提交数据",formData)
            httpAction(httpurl,formData,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })
          }

        })
      },
      handleCancel () {
        this.close()
      },
      popupCallback(row){
        this.form.setFieldsValue(pick(row,'image','name','assetType','specifications','unit','jiexian'))
      },


    }
  }
</script>